package com.stwitinc.redmine.dao.issue;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.stwitinc.data.QueryBuilder;
import com.stwitinc.redmine.NeteaseUtil;
import com.stwitinc.redmine.RedmineCommonUtil;
import com.stwitinc.redmine.dao.DAO;
import com.stwitinc.redmine.dao.IssueDAO;

public class IssueDAOImpl extends DAO implements IssueDAO
{

	public Map<Integer, String>mpUser;
	
	public String getUserIds(){
		
		if(this.mpUser==null ) {
				
		this.mpUser=new HashMap<Integer, String>();
		}
		
		this.mpUser.put(10, "钱淳");
		this.mpUser.put(11, "巨淑展");
		this.mpUser.put(13, "段贝贝");
		
		this.mpUser.put(15, "张聪");
		this.mpUser.put(20, "丁小丽");
		this.mpUser.put(32, "王飞");
		this.mpUser.put(33, "杜君涛");
		this.mpUser.put(38, "朱亮");
		
		this.mpUser.put(40, "耿树生");
		
		this.mpUser.put(51, "田成");
		this.mpUser.put(52, "魏海飞");
		this.mpUser.put(56, "蒋梦尧");
		
		this.mpUser.put(59, "叶伟");
		
		this.mpUser.put(60, "曹静坤");
		
		this.mpUser.put(61, "张宇能");
		this.mpUser.put(66, "刘迪");
		this.mpUser.put(69, "施皓天");
		this.mpUser.put(70, "陈必成");
		//this.mpUser.put(71, "孙正明");


		String strIdString="";
		
		for(Integer id:this.mpUser.keySet()){ 
			strIdString+=id+",";
		}
		
		strIdString=strIdString.substring(0, strIdString.length()-1);
		
		return strIdString;
	}
	
	public void informForTooManyTasks(){
		JSONObject object=this.getIssuseStatus();
		if(object==null) return;
		
		JSONArray userArray=object.getJSONArray("retArray");
		if(userArray==null) return;
		
		NeteaseUtil neteaseUtil=new NeteaseUtil();
		
		for(int i=0;i<userArray.size();i++){
			JSONObject userObj=userArray.getJSONObject(i);
			if(userObj==null) continue;
			
			/*tk.put("featureTotalCount", featureTotalCount);
			tk.put("bugTotalCount", bugTotalCount);
			
			tk.put("personalTotal", featureTotalCount+bugTotalCount);*/
			
			int iPersonalTotal=Integer.parseInt(userObj.getString("personalTotal"));
			if(iPersonalTotal>7){
				int iFeatureTotalCount=Integer.parseInt(userObj.getString("featureTotalCount"));
				int iBugTotalCount=Integer.parseInt(userObj.getString("bugTotalCount"));
				String taskInfo="总任务："+iPersonalTotal+"个。 其中，需求 "+iFeatureTotalCount+" 个，Bug "+iBugTotalCount+" 个";
				
				//根据用户id获取用户手机号
			
				
				String userMobile="13815415011";
				
				/*
				JSONObject user=new RedmineCommonUtil().getUserById(userObj.getString("id"));
				if(user!=null){
					userMobile=user.getString("mobile");
				}*/
				
				//informForTooManyTasks(String name, String mobile, String content);
				if(userMobile!=null && userMobile.length()>0){
					/*neteaseUtil.informForTooManyTasks(user.getString("developer"), userMobile, taskInfo);*/
					neteaseUtil.informForTooManyTasks(userObj.getString("developer"), userMobile, taskInfo);
				}
			}
		}
	}
	
	//在检查中直接调用
	public void informForTooManyTasks(JSONArray array){
		/*JSONObject object=this.getIssuseStatus();
		if(object==null) return;
		
		JSONArray userArray=object.getJSONArray("retArray");*/
		
		JSONArray userArray=array;
		
		if(userArray==null) return;
		
		NeteaseUtil neteaseUtil=new NeteaseUtil();
		
		for(int i=0;i<userArray.size();i++){
			JSONObject userObj=userArray.getJSONObject(i);
			if(userObj==null) continue;
			
			/*tk.put("featureTotalCount", featureTotalCount);
			tk.put("bugTotalCount", bugTotalCount);
			
			tk.put("personalTotal", featureTotalCount+bugTotalCount);*/
			
			int iPersonalTotal=Integer.parseInt(userObj.getString("personalTotal"));
			if(iPersonalTotal>3){
				int iFeatureTotalCount=Integer.parseInt(userObj.getString("featureTotalCount"));
				int iBugTotalCount=Integer.parseInt(userObj.getString("bugTotalCount"));
				String taskInfo="总任务："+iPersonalTotal+"个。 其中，需求 "+iFeatureTotalCount+" 个，Bug "+iBugTotalCount+" 个";
				
				//根据用户id获取用户手机号
				String userMobile="13815415011";
				
				/*
				JSONObject user=new RedmineCommonUtil().getUserById(userObj.getString("id"));
				if(user!=null){
					userMobile=user.getString("mobile");
				}*/
				
				//informForTooManyTasks(String name, String mobile, String content);
				if(userMobile!=null && userMobile.length()>0){
					/*neteaseUtil.informForTooManyTasks(user.getString("developer"), userMobile, taskInfo);*/
					neteaseUtil.informForTooManyTasks(userObj.getString("developer"), userMobile, taskInfo);
				}
			}
		}
	}
	
	//在检查中直接调用
		public void informForDirector(JSONObject object){
			/*JSONObject object=this.getIssuseStatus();
			if(object==null) return;
			
			JSONArray userArray=object.getJSONArray("retArray");*/
			
			//JSONArray userArray=array;
			JSONObject taskInfoObject=object;
			
			if(taskInfoObject==null) return;
			
			NeteaseUtil neteaseUtil=new NeteaseUtil();
			
		
			
			int iFeatureTotalCount=Integer.parseInt(taskInfoObject.getString("featureTotalCount"));
			int iBugTotalCount=Integer.parseInt(taskInfoObject.getString("bugTotalCount"));
			int iBugTotalRejectedCount=Integer.parseInt(taskInfoObject.getString("bugTotalRejectedCount"));
			
			String taskInfo="STW 项目总任务数："+(iFeatureTotalCount+iBugTotalCount)+"个。 其中，需求 "+iFeatureTotalCount
					+" 个，Bug "+iBugTotalCount+" 个，打回的 bug "+iBugTotalRejectedCount+" 个";
			
			neteaseUtil.informForTooManyTasks("领导", "13815415011", taskInfo);
		}
		
	
	@Override
	public JSONObject getIssuseStatus()
	{
		
	/*	String sqlFeatureNew="select concat(c.lastname,c.firstname) as developer, c.id as user_id, count(tracker_id) as feature_new_count  from issues a "+
		"left join projects b on a.project_id=b.id "
				+"left join users c on a.assigned_to_id=c.id "
		+"where a.project_id=8   and a.status_id in (1)  and a.tracker_id=2 "
				+"and c.id in(10,11,13,15,20,24,29,32,33,38,40,42) "
		+"and c.id in ( "+ userIdString +" ) "
		+"and a.created_on >'2017-02-28' "
				+"group by developer  order by  feature_new_count  desc ";*/
		
		String sql="select concat(b.lastname,b.firstname) as developer, b.id, "
				+"sum(case when status_id=1 and tracker_id=2 then 1 else 0 end) feature_new_count, "
				+"sum(case when status_id=2 and tracker_id=2 then 1 else 0 end) feature_inprogress_count, "
				+"sum(case when status_id=4 and tracker_id=2 then 1 else 0 end) feature_feedback_count, "
				+"sum(case when status_id=1 and tracker_id=1 then 1 else 0 end) bug_new_count, "
				+"sum(case when status_id=2 and tracker_id=1 then 1 else 0 end) bug_inprogress_count, "
				+"sum(case when status_id=4 and tracker_id=1 then 1 else 0 end) bug_feedback_count, "
				+"sum(case when status_id=6 and tracker_id=1 then 1 else 0 end) bug_rejected_count "
				+"from issues a , users b "
				+"where a.assigned_to_id=b.id and a.project_id=8 "
				+"and a.assigned_to_id in (10,11,13,15,20,32,33,38,40,51,52,56,59,60,61,66,69,70,71)  "
				+"group by developer ";
		
		JSONObject retObject=new JSONObject();//retArray; retSum

		JSONArray retArray=new JSONArray();
		
		int iTotalFeatureCount=0;
		int iTotalBugCount=0;
		int iTotalBugRejected=0;
		
		JSONObject objSum=new JSONObject();
		
		try
		{
			JSONArray arrTask=executeQuery(new QueryBuilder(sql)).getJSONArray("records");
			if(arrTask!=null && arrTask.size()>0){
				for(int i=0;i<arrTask.size();i++){
					JSONObject tk=(JSONObject) arrTask.get(i);
					if(tk!=null){
						int i_feature_new_count=Integer.parseInt(tk.getString("featureNewCount"));
						int i_feature_inprogress_count=Integer.parseInt(tk.getString("featureInprogressCount"));
						int i_feature_feedback_count=Integer.parseInt(tk.getString("featureFeedbackCount"));
						
						int i_bug_new_count=Integer.parseInt(tk.getString("bugNewCount"));
						int i_bug_inprogress_count=Integer.parseInt(tk.getString("bugInprogressCount"));
						int i_bug_feedback_count=Integer.parseInt(tk.getString("bugFeedbackCount"));
						int i_bug_rejected_count=Integer.parseInt(tk.getString("bugRejectedCount"));
						
						int featureTotalCount=i_feature_new_count+i_feature_inprogress_count+i_feature_feedback_count;
						int bugTotalCount=i_bug_new_count+i_bug_inprogress_count+i_bug_feedback_count+i_bug_rejected_count;
						
						tk.put("featureTotalCount", featureTotalCount);
						tk.put("bugTotalCount", bugTotalCount);
						
						tk.put("personalTotal", featureTotalCount+bugTotalCount);
						
						retArray.add(tk);
						
						
						iTotalFeatureCount+=i_feature_new_count+i_feature_inprogress_count+i_feature_feedback_count;
						iTotalBugCount+=i_bug_new_count+i_bug_inprogress_count+i_bug_feedback_count+i_bug_rejected_count;
						
						iTotalBugRejected+=i_bug_rejected_count;	

					}
				}//for
				
				retArray=RedmineCommonUtil.jsonSortByInteger(retArray, "personalTotal");
				retObject.put("retArray", retArray);//每个人的统计数据
				
				objSum.put("featureTotalCount", iTotalFeatureCount);
				objSum.put("bugTotalCount", iTotalBugCount);
				objSum.put("bugTotalRejectedCount", iTotalBugRejected);
				
				retObject.put("retSum", objSum);
				
				
				Date dtNow=new Date();
				Date lastDateTime=RedmineCommonUtil.lastInformForTooManyTasksTime;
				if(dtNow.getTime()-lastDateTime.getTime()>=RedmineCommonUtil.informForTooManyTasks_Thread_Interval){
					informForTooManyTasks(retArray);
					RedmineCommonUtil.lastInformForTooManyTasksTime=dtNow;
				}
				
				int iHours=dtNow.getHours();
				if(iHours==RedmineCommonUtil.time1 || iHours==RedmineCommonUtil.time2){
					int iMinutes=dtNow.getMinutes();
					if(iMinutes<2){
						informForDirector(objSum);
					}
				}
		/*		//dtNow.getMinutes()>
				System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"+dtNow.getSeconds());
				*/
				
				
			}//if(arrTask!=null && arrTask.size()>0){
			
			
		}
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
		
		return retObject;
	}
	
}
